package com.example.mobilesafe.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Vibrator;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.mobilesafe.R;
import com.example.mobilesafe.dao.AddressDao;

public class QueryAddressActivity extends AppCompatActivity {

    private EditText et_phone;

    private Button btn_query;

    private TextView tv_query_result;

    private String mAddress;

    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            // 4.控制使用查询结果
            tv_query_result.setText(mAddress);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_query_address);

        // 初始化UI
        initUI();
    }

    private void initUI() {
        et_phone = findViewById(R.id.et_phone);
        btn_query = findViewById(R.id.btn_query);
        tv_query_result = findViewById(R.id.tv_query_result);

        // 1.点击查询功能，注册按钮的点击事件
        btn_query.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phone = et_phone.getText().toString();
                if (!TextUtils.isEmpty(phone)){
                    // 2.查询是耗时操作，需要开启子线程
                    query(phone);
                }else {
                    Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.shake);
                    et_phone.startAnimation(animation);
                    // 手机震动效果
                    Vibrator vibrator = (android.os.Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
                    vibrator.vibrate(2000); // 震动毫秒值
                    vibrator.vibrate(new long[]{2000,5000,2000,5000},-1); // 规律震动（震动规则（不震动时间，震动时间，不震动时间，震动时间，...），重复次数（-1表示不要重复震动））
                }
            }
        });

        // 5.实时查询
        et_phone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                // 文本发生改变前
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // 文本发生改变时
            }

            @Override
            public void afterTextChanged(Editable s) {
                // 文本发生改变后
                String phone = et_phone.getText().toString();
                query(phone);
            }
        });

    }

    /**
     * 查询操作，在子线程中
     * @param phone 查询电话号码
     */
    private void query(final String phone) {
        new Thread(){
            @Override
            public void run() {
                mAddress = AddressDao.getAddress(phone);
                // 3.消息机制，告知主线程查询结束，可以去使用查询结果
                mHandler.sendEmptyMessage(0);
            }
        }.start();
    }
}
